package sel.me.sharding.learning.algorithm;

import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;

import java.util.Collection;

/**
 * 分表算法:
 * 只支持==和IN的分表算法实现
 * PreciseShardingAlgorithm<Integer>  必须和分片键字段类型匹配
 * @author 15068
 *
 */
public final class TablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {


    @Override
    public String doSharding(final Collection<String> availableTargetNames, final PreciseShardingValue<Integer> shardingValue) {
        int size = availableTargetNames.size();
        for (String each : availableTargetNames) {
            if ( each.endsWith( String.valueOf(shardingValue.getValue() % size) ) ) {
                return each;
            }
        }
        throw new UnsupportedOperationException();
    }
    
}
